import _ from 'lodash'

/**
 * 初始化订阅列表
 */
export const _onMicroInitializedHandlers: Function[] = []
/**
 * 激活定义列表
 */
export const _onMicroActivatedHandlers: Function[] = []
/**
 * 失活定义列表
 */
export const _onMicroDeactivatedHandlers: Function[] = []

/**
 * 订阅触发器
 * @param handlers
 * @returns
 */
export function _subscriptTrigger(handlers: Function[]) {
    if (!handlers.length) return
    let i = 0
    for (i; i < handlers.length; i++) {
        const handler = handlers[i]
        if (_.isFunction(handler)) {
            handler()
            handlers.splice(i, 1)
            i--
        }
    }
}

/**
 * 订阅微应用首次挂载，只会执行一次
 */
export function onMicroInitialized(cb: Function) {
    _onMicroInitializedHandlers.push(cb)
}

/**
 * 订阅微应用激活
 */
export function onMicroActivated(cb: Function) {
    _onMicroActivatedHandlers.push(cb)
}

/**
 * 订阅微应用失活
 */
export function onMicroDeactivated(cb: Function) {
    _onMicroDeactivatedHandlers.push(cb)
}
